//
//  ChatPicCell.m
//  学生汇
//
//  Created by yull on 14/11/12.
//  Copyright (c) 2014年 于博. All rights reserved.
//

#define CHAT_PIC_CELL_X_SPACE 10
#define CHAT_PIC_CELL_Y_SPACE 10

#import "ChatPicCell.h"
#import "UIImageView+WebCache.h"

@implementation ChatPicCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        CGRect rect = CGRectMake(0, 0, 80, 80);
        
        self.chatImageView = [[UIImageView alloc] initWithFrame:rect];
        self.chatImageView.layer.cornerRadius = 5.0f;
        self.chatImageView.layer.masksToBounds = YES;
        self.chatImageView.userInteractionEnabled = YES;
        self.chatImageView.backgroundColor = [UIColor clearColor];
        [self.chatContentView addSubview:self.chatImageView];
        
        self.shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.chatContentView.width, 0)];
        self.shadowView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.shadowView.backgroundColor = [UIColor blackColor];
        self.shadowView.alpha = 0.6f;
        self.shadowView.layer.cornerRadius = 5.0f;
        [self.chatContentView addSubview:self.shadowView];
        
        self.percentLabel = [[UILabel alloc] initWithFrame:self.chatContentView.bounds];
        self.percentLabel.textColor = [UIColor whiteColor];
        self.percentLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        self.percentLabel.textAlignment = NSTextAlignmentCenter;
        [self.chatContentView addSubview:self.percentLabel];
        
        self.xSpace = 7;
        self.ySpace = 3;
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageTapGesture:)];
        [self.chatImageView addGestureRecognizer:tapGesture];
    }
    return self;
}

+ (CGFloat)calculateImageCellHeight
{
    return 80.0f + 16 * 2 + CHAT_PIC_CELL_Y_SPACE;
}

- (void)drawCellWithChatMsg:(ChatBaseMsg *)chatMsg
{
    self.chatContentView.width = self.chatImageView.width;
    self.chatContentView.height = self.chatImageView.height;
    
    [super drawCellWithChatMsg:chatMsg];
    
    if (chatMsg.roleType == ChatMsgRoleTypeReceive) {
        self.failImageView.left = self.contentImageView.right + 5;
//        self.sendingImageView.left = self.contentImageView.right + 5;
        
        if (chatMsg.sendStatus == ChatMsgSendStatusSending) {
            NSInteger percent = (NSInteger)(chatMsg.picUploadPercent * 100);
            self.percentLabel.text = [NSString stringWithFormat:@"%d%%",percent];
            self.percentLabel.hidden = NO;
            
            self.shadowView.height = (100 - percent) *self.chatContentView.height / 100;
        }else{
            self.percentLabel.hidden = YES;
            self.shadowView.hidden = YES;
        }
        
        [self.chatImageView sd_setImageWithURL:[NSURL URLWithString:chatMsg.url] placeholderImage:[UIImage imageNamed:@"chat_img_default_gray"]];
    }else{
        self.failImageView.right = self.contentImageView.left - 5;
//        self.sendingImageView.right = self.contentImageView.left - 5;
        
        if (chatMsg.localPath.length > 0) {
//            NSData *data = [NSData dataWithContentsOfFile:chatMsg.localPath];
//            self.chatImageView.image = [UIImage imageWithContentsOfFile:chatMsg.localPath];
            [self.chatImageView sd_setImageWithURL:[NSURL fileURLWithPath:chatMsg.localPath]];
        }else {
           [self.chatImageView sd_setImageWithURL:[NSURL URLWithString:chatMsg.url] placeholderImage:[UIImage imageNamed:@"chat_img_default_white"]];
        }
    }
}

- (void)onImageTapGesture:(UITapGestureRecognizer *)tapGesture
{
    if (self.chatMsg.sendStatus == ChatMsgSendStatusFail) {
        [super onTapGesture:nil];
    }else {
        if ([self.delegate respondsToSelector:@selector(picClickChatMsg:image:)]) {
            [self.delegate picClickChatMsg:self.chatMsg image:self.chatImageView.image];
        }
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    
    ChatBaseMsg *message = (ChatBaseMsg *)object;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([keyPath isEqualToString:@"sendStatus"]) {
            if (message.sendStatus == ChatMsgSendStatusSending) {
                self.percentLabel.hidden = NO;
                self.shadowView.hidden = NO;
            }else {
                self.percentLabel.hidden = YES;
                self.shadowView.hidden = YES;
            }
        }else if ([keyPath isEqualToString:@"picUploadPercent"]) {
            NSInteger percent = (NSInteger)(message.picUploadPercent);
            if (percent == 100) {
                self.percentLabel.hidden = YES;
                self.shadowView.hidden = YES;
            }else {
                self.percentLabel.text = [NSString stringWithFormat:@"%d%%",percent];
                self.percentLabel.hidden = NO;
                self.shadowView.hidden = NO;
                self.shadowView.height = (100 - percent) *self.chatContentView.height / 100;
            }
        }
    });
}

/*
+ (CGSize)getImageSizeFromMessage:(ChatBaseMsg *)message
{
    NSArray *strArray = [message.msgLength componentsSeparatedByString:@"."];
    
    if (strArray.count != 2) {
        return CGSizeMake(80, 80);
    }
    
    CGFloat width = [[strArray objectAtIndex:0] floatValue];
    CGFloat height = [[strArray objectAtIndex:1] floatValue];
    
    if (width == 0 || height == 0) {
        return CGSizeMake(80, 80);
    }
    
    CGFloat scaleWidth = width / 150;
    CGFloat scaleHeight = height / 150;
    
    CGFloat scale = MAX(scaleHeight, scaleWidth);
    
    return CGSizeMake(width / scale, height / scale);
}
 */
@end
